<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where" @keydown.enter.prevent="reload">
          <a-row>
            <a-form-item label="客户名称:">
              <a-input style="width: 160px;margin-right: 10px;" v-model:value.trim="where.customerName" placeholder="请输入客户名称" allow-clear />
            </a-form-item>
            <a-form-item label="联系人姓名:">
              <a-input style="width: 160px;margin-right: 10px;" v-model:value.trim="where.name" placeholder="请输入联系人姓名" allow-clear />
            </a-form-item>
            <a-form-item label="联系人电话:">
              <a-input style="width: 160px;margin-right: 10px;" v-model:value.trim="where.phone" placeholder="请输入联系人电话" allow-clear />
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="id"
          :datasource="url"
          :columns="columns"
          :where="where"
        >
          <!-- table上边工具栏 -->
          <!--          <template #toolbar>
                      <a-space>
                        <a-button type="primary" @click="openEdit()">
                          <template #icon>
                            <plus-outlined />
                          </template>
                          <span>新建</span>
                        </a-button>
                      </a-space>
                    </template>-->

          <!-- table列表状态栏 -->
          <!-- 1是激活，2是禁用 -->
          <template #state="{ record }">
            <a-switch :checked="record.statusFlag === 1" @change="checked => editState(checked, record)" />
          </template>

          <template #customerName="{ record }">
            <a-tooltip :title="record.customerName" placement="topLeft">
              {{ record.customerName || '--' }}
            </a-tooltip>
          </template>

          <template #remark="{ record }">
            <a-tooltip :title="record.remark" placement="topLeft">
              {{ record.remark || '--' }}
            </a-tooltip>
          </template>
          <template #contact="{ record }">
            <span style="display: block;">姓名：{{ record.name }}</span>
            <span style="display: block;">电话：{{ record.phone }}</span>
            <span style="display: block;">邮箱：{{ record.email }}</span>
          </template>
          <!-- table操作栏按钮 -->
          <template #action="{ record }">
            <a-space>
              <a @click="openEdit(record)">详情</a>
              <!--              <a-divider type="vertical" />
                            <a-popconfirm title="确定要重置此账号密码吗？" @confirm=" resetPassword(record)">
                              <a class="ele-text-danger">联系人</a>
                            </a-popconfirm>-->
            </a-space>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
  </div>

  <!-- 编辑弹窗 -->
  <contacts-edit v-model:visible="showEdit" :data="current" @done="reload" />
</template>

<script>
// 删除，感叹号，加号图标
import contactsEdit from './contacts-edit.vue';
import { CustomerContactsApi } from '@/api/CustomerContactsApi';

export default {
  name: 'contacts_info',
  components: {
    contactsEdit
  },
  /*  setup(){
      this.where = {customerName: 'asdjbasd123123'};

    },*/

  data() {
    return {
      // 表格数据接口
      url: '/business/api/customer/contacts/findPage',
      // 表格列配置
      columns: [
        {
          title: '序号',
          key: 'index',
          width: 48,
          customRender: ({ index }) => this.$refs.table.tableIndex + index
        },
        {
          title: '联系人信息',
          width: 280,
          slots: { customRender: 'contact' }
        },
        {
          title: '所属客户',
          dataIndex: 'customerName',
          width: 200,
          sorter: false,
          ellipsis: true,
          slots: { customRender: 'customerName' }
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 160,
          sorter: false,
          ellipsis: true,
          slots: { customRender: 'remark' }
        },
        /*{
          title: '操作时间',
          dataIndex: 'updateTime',
          sorter: true,
          width: 160
        },*/
        {
          title: '状态',
          dataIndex: 'contactStatusName',
          sorter: false,
          width: 160
        },
/*        {
          title: '状态',
          dataIndex: 'status',
          sorter: false,
          align: 'center',
          width: 160,
          slots: { customRender: 'state' }
        },*/
        {
          title: '操作',
          key: 'action',
          width: 180,
          align: 'center',
          fixed: 'right',
          slots: { customRender: 'action' }
        }
      ],
      // 表格搜索条件
      where: { customerName: this.$route.params.customerName },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },

  watch: {
    '$route.params.customerName': {
      handler(newVal) {
        console.log(newVal);
        // 监听路由参数变化并更新where.customerName
        this.where.customerName = newVal;
        // 触发搜索或过滤逻辑...
        setTimeout(() => {
          if (this.$refs.table) {
            this.$refs.table.reload({ page: 1 });
          }
        });
      },
      immediate: true // 确保在初始加载时也能执行一次
    }
  },

  methods: {
    /**
     * 搜索按钮
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     */
    reset() {
      this.where = {};
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除单个
     */
    async remove(row) {
      const result = await CustomerContactsApi.del({ id: row.id });
      this.$message.success(result.message);
      this.reload();
    },

    /**
     * 打开编辑弹窗
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    /**
     * 修改状态
     */
    async editState(checked, row) {
      const id = row.id;
      // 状态：1-启用，2-禁用
      const statusFlag = checked ? 1 : 2;
      const result = await CustomerContactsApi.edit({ id, statusFlag });
      this.$message.success(result.message);
      row.statusFlag = statusFlag;
    }
  }
};
</script>

<style scoped>
/*搜索框与表格之间的间隙*/
.block-interval {
  margin-bottom: 10px;
}

/*重写顶部搜索表单的样式*/
.ant-form-item {
  margin-bottom: 0;
}
/* 表格斑马样式 **/
/deep/.ant-table-tbody tr:nth-child(2n)
{
  background-color:#fafafa;
}
</style>
